package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

// 收货地址
@RestController
public class AddressController {

    @Autowired
    private AddressService addressService;


    // 地址列表
    @GetMapping("/address/list")
    public ResultInfo findList() {
        // 调用service
        List<Address> addressList = addressService.findList();
        // 返回结果
        return ResultInfo.success(addressList);
    }

    // 新增地址
    @PostMapping("/address")
    public ResultInfo save(@RequestBody Address address) { // 接收请求参数
        // 调用service新增
        addressService.save(address);
        // 返回结果
        return ResultInfo.success();
    }

    //把地址设置
    @PutMapping("/address/default")
    public ResultInfo setDefault(@RequestBody Map<String, Long> param) {
        //调用
        addressService.setDefault(param);
        //返回
        return ResultInfo.success();
    }


    //查询(自动跳转)默认地址
    @GetMapping("/address/default")
    public ResultInfo findDefault() {
        //调用
        Address address = addressService.findDefault();
        return ResultInfo.success(address);
    }

    //回显地址(修改前)
    @GetMapping("/address/{id}")
    public ResultInfo findByAddressId(@PathVariable Long id){
        //调用
        Address address = addressService.findByAddressId(id);
        return ResultInfo.success(address);
    }

    //修改地址
    @PutMapping("/address")
    public ResultInfo updateByAddress(@RequestBody Address address){
        //   以推翻 × 调用public void save(Address address) {}之前写好的的新增方法\
        //调用service
        addressService.updateByAddress(address);
        //返回
        return ResultInfo.success();
    }

    //删除地址
    @DeleteMapping("/address")
    public ResultInfo deleteById(Long ids){
        //调用
        addressService.deleteById(ids);
        //返回
        return ResultInfo.success();
    }




}
